package com.xuecheng.controller;

import com.xuecheng.dto.BindTeachplanMediaDTO;
import com.xuecheng.dto.SaveTeachplanDTO;
import com.xuecheng.service.TeachplanService;
import com.xuecheng.vo.TeachplanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程计划
 */
@Slf4j
@RestController
@RequestMapping("/teachplan")
@Api(value = "课程计划", tags = "课程计划")
public class TeachplanController {
	
	@Resource
	private TeachplanService teachplanService;
	
	/**
	 * 查询课程计划树形结构
	 */
	@GetMapping("/{id}/tree-nodes")
	@ApiOperation("查询课程计划")
	public List<TeachplanVO> getTeachplanTree(@PathVariable Long id) {
		log.info("查询课程计划id: {}", id);
		return teachplanService.findTeachplanTree(id);
	}
	
	/**
	 * 新增/更新课程计划
	 */
	@PostMapping
	@ApiOperation("新增/更新课程计划")
	public void addOrPutTeachplan(@RequestBody SaveTeachplanDTO saveTeachplanDTO) {
		log.info("新增/更新课程计划: {}", saveTeachplanDTO);
		teachplanService.saveTeachplan(saveTeachplanDTO);
	}
	
	/**
	 * 删除课程计划
	 */
	@DeleteMapping("/{id}")
	@ApiOperation("删除课程计划")
	public void deleteTeachplan(@PathVariable Long id) {
		log.info("删除课程计划id: {}", id);
		teachplanService.removeById(id);
	}
	
	/**
	 * 修改排序
	 */
	@PostMapping("/{type}/{id}")
	@ApiOperation("修改排序")
	public void putTeachplanSort(@PathVariable Integer type, @PathVariable Long id) {
		log.info("修改排序id：{}，type：{}", id, type);
		teachplanService.saveTeachplan(SaveTeachplanDTO.builder().grade(type).id(id).build());
	}
	
	/**
	 * 课程计划和媒资信息绑定
	 */
	@PostMapping("/association/media")
	@ApiOperation("课程计划和媒资信息绑定")
	public void associationMedia(@RequestBody BindTeachplanMediaDTO bindTeachplanMediaDTO) {
		log.info("课程计划和媒资信息绑定: {}", bindTeachplanMediaDTO);
		teachplanService.associationMedia(bindTeachplanMediaDTO);
	}
	
	/**
	 * 删除绑定信息
	 */
	@DeleteMapping("/association/media/{teachPlanId}/{mediaId}")
	public void deleteAssociationMedia(@PathVariable("teachPlanId") Long teachPlanId,
	                                   @PathVariable("mediaId") String mediaId) {
		log.info("删除绑定信息: teachPlanId: {}, mediaId: {}", teachPlanId, mediaId);
		teachplanService.associationMedia(BindTeachplanMediaDTO.builder().teachplanId(teachPlanId).mediaId(mediaId).build());
	}
}
